(function (window) {
    var arr = [],
        push = arr.push,
        slice = arr.slice;

    function Itcast(selesector) {
        return new Itcast.fn.init(selesector);
    }

    Itcast.fn = Itcast.prototype = {
        constructor: Itcast,
        length: 0,
        init: function (selector) {
            if (!selector)return this;
            push.apply(this, Itcast.select(selector));
        }
    };
    Itcast.fn.init.prototype = Itcast.fn;
    Itcast.extend = Itcast.fn.extend = function (obj) {
        for (var k in obj) {
            this[k] = obj[k];
        }
        return this;
    };
    Itcast.extend({
        select: function (selector) {
            return document.querySelectorAll(selector);
        },
        isArrayLike: function (obj) {
            if (Object.prototype.toString.call(obj) == "[object Array]") {
                return true;
            }
            var length = "lenght" in obj && obj.length;
            return typeof  length === "number" && length >= 0;
        },
        each: function (arr, callback) {
            if (Itcast.isArrayLike(arr)) {
                for (var i = 0; i < arr.length; i++) {
                    if (callback.call(arr[i], i, arr[i]) === false)break;
                }
            } else {
                for (var k in arr) {
                    if (callback.call(arr[k], k, arr[k]) === false)break;
                }
            }
            return arr;
        },
        map: function (arr, callback) {
            var newArr = [], tmp;
            if (Itcast.isArrayLike(arr)) {
                for (var i = 0; i < arr.length; i++) {
                    tmp = callback(arr[i], i);
                    if (tmp != null) {
                        newArr.push(tmp);
                    }
                }
            } else {
                for (var k in arr) {
                    tmp = callback(arr[k], k);
                    if (tmp != null) {
                        newArr.push(tmp);
                    }
                }
            }
            return newArr;
        }
    });
    Itcast.fn.extend({
        toArray: function () {
            return slice.call(this);
        },
        get: function (index) {
            if (index === undefined) {
                return this.toArray();
            }
            if (index < 0) {
                return this[this.length + index];
            } else {
                return this[index];
            }
        },
        eq:function(i){
            var iobj=this.constructor();
            if(i==null) return iobj;
            var dom=this.get(i);
            if(dom){
                iobj[0]=dom
                iobj.length=1;
            }
            return iobj;
        },
        first: function () {
            return this.eq(0);
        },
        last: function () {
            return this.eq(-1);
        },
    });
    window.Itcast = window.I = Itcast;

})(window);


